package com.siki.config.jsonConfig;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * @Author: Siki
 * @Description: 统一返回实体
 */
@Getter
@Setter
@NoArgsConstructor
public class JsonResult<T> implements Serializable {
    private Boolean success;
    private Integer statusCode;
    private String message;
    private T data;


    private JsonResult(boolean success) {
        this.success = success;
        this.statusCode = success ? GlobalServiceMsgCode.SUCCESS.getCode() : GlobalServiceMsgCode.COMMON_FAIL.getCode();
        this.message = success ? GlobalServiceMsgCode.SUCCESS.getMessage() : GlobalServiceMsgCode.COMMON_FAIL.getMessage();
    }

    private JsonResult(boolean success, GlobalServiceMsgCode resultEnum) {
        this.success = success;
        this.statusCode = success ? GlobalServiceMsgCode.SUCCESS.getCode() : (resultEnum == null ? GlobalServiceMsgCode.COMMON_FAIL.getCode() : resultEnum.getCode());
        this.message = success ? GlobalServiceMsgCode.SUCCESS.getMessage() : (resultEnum == null ? GlobalServiceMsgCode.COMMON_FAIL.getMessage() : resultEnum.getMessage());
    }

    private JsonResult(boolean success, T data) {
        this.success = success;
        this.statusCode = success ? GlobalServiceMsgCode.SUCCESS.getCode() : GlobalServiceMsgCode.COMMON_FAIL.getCode();
        this.message = success ? GlobalServiceMsgCode.SUCCESS.getMessage() : GlobalServiceMsgCode.COMMON_FAIL.getMessage();
        this.data = data;
    }

    private JsonResult(boolean success, GlobalServiceMsgCode resultEnum, T data) {
        this.success = success;
        this.statusCode = success ? GlobalServiceMsgCode.SUCCESS.getCode() : (resultEnum == null ? GlobalServiceMsgCode.COMMON_FAIL.getCode() : resultEnum.getCode());
        this.message = success ? GlobalServiceMsgCode.SUCCESS.getMessage() : (resultEnum == null ? GlobalServiceMsgCode.COMMON_FAIL.getMessage() : resultEnum.getMessage());
        this.data = data;
    }

    public static JsonResult success(){
        return new JsonResult(true);
    }

    public static JsonResult success(Object data){
        return new JsonResult(true,data);
    }

    public static JsonResult fail(GlobalServiceMsgCode resultEnum){
        return new JsonResult(false,resultEnum);
    }

    public static JsonResult fail(Object data){
        return new JsonResult(false,data);
    }

    public static JsonResult fail(GlobalServiceMsgCode resultEnum,Object data){
        return new JsonResult(false,resultEnum,data);
    }

}
